package chapter08_class

/**
 * Created with IntelliJ IDEA.
 * ClassName: Test14_Singleton
 * Package: chapter08_class
 * Description:
 * User: fzykd
 *
 * @Author: LQH
 *          Date: 2023-07-03
 *          Time: 20:30
 */

//单列模式
object Test14_Singleton {
  def main(args: Array[String]): Unit = {
    val stu2 = Student14.getInstance()
    stu2.info
    val stu1 = Student14.getInstance()
    println(stu1 == stu2) //true
    println(stu1) //单例的 引用只有一个
    println(stu2)
  }
}


//private 将主构造函数私有化
class Student14 private(val name: String, val age: Int) {
  def info = {
    println(s"name: $name , age: $age , ${Student13.school}")
  }
}
//饿汉模式
//object Student14{
//  //创建单一对象 全局只有一份
//  private val student:Student14 = new Student14("alice",18)
//  def getInstance():Student14 = student
//}

//懒汉模式
object Student14 {

  //不给初始值 使用var创建 并且使用下划线
  private var student: Student14 = _

    def getInstance(): Student14 = {

    if (student == null){
      student = new Student14("bob",88)
    }
    student
  }

}